package com.yuanqi.repository;

import com.yuanqi.domain.OrderPayment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface OrderPaymentRepository extends JpaRepository<OrderPayment, Integer> {
    
    Optional<OrderPayment> findByOrderNumber(String orderNumber);
    
    List<OrderPayment> findByPaymentStatus(Integer paymentStatus);
    
    List<OrderPayment> findByOrderType(Integer orderType);
    
    @Query("SELECT o FROM OrderPayment o WHERE " +
           "(:orderType IS NULL OR o.orderType = :orderType) AND " +
           "(:orderNumber IS NULL OR o.orderNumber LIKE %:orderNumber%) AND " +
           "(:paymentStatus IS NULL OR o.paymentStatus = :paymentStatus)")
    List<OrderPayment> findByConditions(@Param("orderType") Integer orderType,
                                       @Param("orderNumber") String orderNumber,
                                       @Param("paymentStatus") Integer paymentStatus);
    
    @Query("SELECT COUNT(o) FROM OrderPayment o WHERE o.paymentStatus = :status")
    long countByPaymentStatus(@Param("status") Integer status);
}
